package com.breakfast.fun.ui.user.login;

import com.breakfast.fun.Constant;
import com.breakfast.fun.data.entity.user.Login;
import com.breakfast.fun.data.entity.user.Result;
import com.breakfast.fun.util.Api;
import com.breakfast.fun.util.EncyptUtil;
import com.breakfast.fun.util.L;
import com.breakfast.fun.util.T;

import java.io.IOException;
import java.util.Map;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by yukai on 16/9/26.
 */

public class RegisterPresenter extends RegisterContract.Presenter {
    @Override
    public void getCode(String mobile) {
        Map<String, String> map = Api.getInstance().getBaseParams();

        map.put("action", "code2");
        map.put("mobile", mobile);

        showLoadingDialog(true);

        rxManager.add(model.getCodeModel(map)
                .subscribeOn(Schedulers.computation())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new Subscriber<Result>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        hideLoadingDialog();
                        T.show("发送失败");
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(Result result) {
                        hideLoadingDialog();
                        if (result.isSuccess()) {
                            view.sendCodeSuccess();
                        } else {
                            T.show(result.getMsg());
                        }
                    }

                }));
    }

    @Override
    public void register(String mobile, String code, String pwd, String referee, String birthday) {
        Map<String, String> map = Api.getInstance().getBaseParams();

        map.put("action", "register2");
        map.put("mobile", mobile);
        map.put("password", pwd);
        map.put("code", code);
        map.put("referee", referee);
        if (!birthday.equals("")) {
            map.put("birthday", birthday);
        }
        String sign = null;
        try {
            sign = EncyptUtil.getSignature(map, Constant.appSecret);
        } catch (IOException e) {
            e.printStackTrace();
        }
        map.put("sign", sign);

        showLoadingDialog(true);

        rxManager.add(model.getRegisterModel(map)
                .subscribeOn(Schedulers.computation())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new Subscriber<Login>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        hideLoadingDialog();
                        T.show("获取数据失败");
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(Login login) {
                        hideLoadingDialog();
                        if (login.isSuccess()) {
                            view.registerSuccess(login);
                        } else {
                            T.show(login.getMsg());
                        }
                    }

                }));
    }

    @Override
    public void onStart() {

    }
}
